package com.xhtt.modules.app.controller.inspection;

import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.inspection.service.IInsClassesTeamDetailService;
import com.xhtt.modules.inspection.service.IInsTaskService;
import com.xhtt.modules.inspection.service.IInsTaskStepService;
import com.xhtt.modules.inspection.vo.InsTaskVO;
import com.xhtt.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 巡检任务 前端控制器
 * </p>
 *
 * @author xj
 * @since 2019-01-18
 */
@RestController
@RequestMapping("app/ins-task")
@Api(value = "巡检-任务发布", tags = "巡检-任务发布")
public class InsTaskController {

    @Autowired
    private IInsTaskService insTaskService;

    @Autowired
    private IInsClassesTeamDetailService insClassesTeamDetailService;

    @Autowired
    private IInsTaskStepService insTaskStepService;

    @GetMapping(value = "/list")
    @ApiOperation("获取巡检任务列表(web/app)")
    @Login
    public R list(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        if (user.getTeamId() != null) {
            params.put("teamId", user.getTeamId());
        }
        return R.ok().put(Constant.DATA, insTaskService.queryPage(params));
    }


    @PostMapping(value = "/add")
    @ApiOperation("新增巡检任务(web/app)")
    @Login
    public R add(@ApiIgnore @LoginUser SysUserEntity user, @RequestBody InsTaskVO insTaskVO) {
        insTaskVO.setCreateUserId(user.getUserId().intValue());
        if (user.getTeamId() == null) {
            throw new RRException(RRExceptionEnum.USER_NOT_BELONG_GROUP);
        } else {
            insTaskVO.setTeamId(user.getTeamId());
        }
        insTaskService.add(insTaskVO);
        return R.ok();
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除巡检任务(web)")
    public R delete(@RequestBody Integer[] ids) {
        insTaskService.delete(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改巡检任务(web)")
    public R update(@RequestBody InsTaskVO insTaskDTO) {
        insTaskService.update(insTaskDTO);
        return R.ok();
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取巡检任务(web/app)")
    public R get(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, insTaskService.detail(id));
    }


    @GetMapping(value = "/detail")
    @ApiOperation("根据指定日期获取人员以及排班时间段(web)")
    public R getDetail(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insTaskService.queryDetailByDate(params));
    }

    @PostMapping(value = "/scheduling/update")
    @ApiOperation("排班调整保存(web)")
    public R schedulingUpdate(@RequestBody InsTaskVO insTaskVO) {
        insTaskService.schedulingUpdate(insTaskVO);
        return R.ok();
    }

    @GetMapping(value = "/getAlreadyClasses")
    @ApiOperation("获取当前用户已排班的日期(wep/app)")
    @Login
    public R getAlreadyClasses(@ApiIgnore @LoginUser SysUserEntity user, @RequestParam(value = "taskId", required = false) Integer taskId) {

        return R.ok().put(Constant.DATA, insClassesTeamDetailService.getCurrentUserClasses(user, taskId));
    }

}
